import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'model/price_model.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'dart:async' show Future;
import 'package:url_launcher/url_launcher.dart';
import 'function.dart';
import 'util/event.dart';

class PricePage extends StatefulWidget {
  @override
  _PricePageState createState() => new _PricePageState();
}

class _PricePageState extends State<PricePage> {
  Color mainColor = Colors.deepOrange;
  @override
  initState() {
    eventBus.on<Event>().listen((event) async {
      switch (event.msg) {
        case "theme":
          setTheme();
          break;
        case "price":
          refresh_price(false);
      }
    });
    super.initState();
    setTheme();
    new Future.delayed(new Duration(milliseconds: 500), () {
      refresh_price(false);
    });
  }

  setTheme() {
    themeColor.then((color) {
      mainColor = color;
      setState(() {});
    });
  }

  RefreshController _refreshController = new RefreshController();
  refresh_price(bool direction) {

    if (direction == false) {
      if(_refreshController.isRefresh(true)){
        print("刷新着");
        _refreshController.sendBack(true, RefreshStatus.completed);
      }
      _refreshController.requestRefresh(true);

    } else {
      for (PriceModel price in platforms) {
        price.getPrice().then((value) {
          setState(() {});
        });

      }
      new Future.delayed(new Duration(milliseconds: 2000), () {
        _refreshController.sendBack(true, RefreshStatus.completed);
      });
    }
  }

  Widget _buildPrice(PriceModel price) {
    return new Column(
      children: <Widget>[
        new ListTile(
          onTap: () {
            launch(price.invite);
//            Navigator.of(context).push(
//              new CupertinoPageRoute(builder: (context) {
//                return new WebViewPage(price.invite, price.name);
//              }),
//            );
          },
          title: new Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              new Expanded(
                flex: 3,
                child: new Text(
                  price.name,
                  textAlign: TextAlign.center,
                ),
              ),
              new Expanded(
                flex: 2,
                child: new Text(
                  price.current ?? "最新价",
                  textAlign: TextAlign.center,
                  style: new TextStyle(fontSize: 12.0),
                ),
              ),
              new Expanded(
                flex: 3,
                child: new Text(
                  price.amount ?? "交易量",
                  textAlign: TextAlign.center,
                  style: new TextStyle(fontSize: 12.0),
                ),
              ),
              new Expanded(
                flex: 3,
                child: new Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Text(
                      price.min ?? "最低",
                      style: new TextStyle(color: Colors.green, fontSize: 12.0),
                    ),
                    new Text("/"),
                    new Text(
                      price.max ?? "最高",
                      style: new TextStyle(color: Colors.red, fontSize: 12.0),
                    ),
                  ],
                ),
              ),
              new Expanded(
                flex: 2,
                child: new Text(
                  (price.change ?? "涨跌") + "%",
                  style: new TextStyle(
                      color: price.upDown ? Colors.red : Colors.green,
                      fontSize: 12.0),
                  textAlign: TextAlign.center,
                ),
              ),
            ],
          ),
        ),
        Divider(height: 1.0),
      ],
    );
  }

  Widget _pulldownBuilder(context, mode) {
    return new ClassicIndicator(
      mode: mode,
      idleText: "继续下拉刷新",
      completeText: "刷新完成",
      refreshingText: "刷新中…",
      releaseText: "松手开始刷新",
      refreshingIcon: const CupertinoActivityIndicator(),
      completeIcon: const Icon(Icons.done, size: 22.0, color: Colors.green),
      height: 22.0,
    );
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: new ThemeData(
        accentColor: mainColor,
        primaryColor: mainColor,
      ),
      home: new Scaffold(
        appBar: new CupertinoNavigationBar(
          actionsForegroundColor: Colors.white,
          backgroundColor: mainColor,
          middle: new Text(
            "币价行情",
            style: new TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.normal,
            ),
          ),
        ),
        body: new Container(
          child: new Column(
            children: <Widget>[
              new Expanded(
                child: new Scrollbar(
                  child: new SmartRefresher(
                    enablePullDown: true,
                    onRefresh: refresh_price,
                    headerBuilder: _pulldownBuilder,
                    controller: _refreshController,
                    child: new ListView.builder(
                      itemCount: platforms.length,
                      itemBuilder: (context, i) => _buildPrice(platforms[i]),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
//          child:
      ),
    );
  }
}
